# -*- coding:utf-8 -*-
"""
Created on 2016/3/23 21:11
@File:search.py
@Author:Liangrong Li
@
"""
import urllib
import urllib2
import json


def search_domain(pre_word):
    url = 'http://m.cndns.com/ajax/domains.ashx'
    headers = {'Host': 'm.cndns.com',
               'User-Agent': 'Googlebot/2.1 (+http://www.google.com/bot.html)',
               'Accept': 'application/json, text/javascript, */*; q=0.01',
               'Accept-Language': 'zh-cn,zh;q=0.8,en-us;q=0.5,en;q=0.3',
               'Accept-Encoding': 'gzip, deflate',
               'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
               'X-Requested-With': 'XMLHttpRequest',
               'Pragma': 'no-cache',
               'Cache-Control': 'no-cache',
               'Referer': 'http://m.cndns.com/domains/search.aspx',
               'Content-Length': '44',
               'Cookie': 'cndnscookieid=0abo2dvgvcrg5wld1hihusry; s_pers=%20s_vnum%3D1459440000756%2526vn%253D1%7C1459440000756%3B%20s_fid%3D24D0E6648313928E-1A8658A3412EEEA4%7C1521810940669%3B%20s_nr%3D1458738940685-Repeat%7C1461330940685%3B%20s_invisit%3Dtrue%7C1458740740695%3B; cndnssessionid=b4f59b709a9b60cf13b1d4ea30d59444; visitfrom=95707954557901392987299159495056; s_sess=%20s_cc%3Dtrue%3B%20s_evar8%3D9%253A00PM%3B%20s_evar9%3DWednesday%3B%20s_evar6%3DRepeat%3B%20s_evar7%3D1%3B%20s_sq%3Dveristemplate%253D%252526pid%25253DShanghai%25252520Meicheng%2525253Apc%2525253AShanghai%25252520Meicheng%25252520Registration%25252520page%252526pidt%25253D1%252526oid%25253Dfunctiononclick%25252528event%25252529%2525257BshowMoreDomain%25252528%25252529%2525253B%2525257D%252526oidt%25253D2%252526ot%25253DUL%3B; ASP.NET_SessionId=gca5fvk5q2oekvlmcofgd3fa; locationUrl=http%3a%2f%2fm.cndns.com%2fdefault.aspx; refercndns=directcndns; CNZZDATA1253521043=1925609185-1458736607-%7C1458736607',
               'Connection': 'keep-alive'
               }
    para_data = {'pre': pre_word, 'ext': 'com', 'domtpe': '1', 'module': 'checkdom'}
    para_dict = urllib.urlencode(para_data)
    req = urllib2.Request(url, para_dict, headers=headers)
    try:
        resp = urllib2.urlopen(req, timeout=10)
        html = resp.read()
        json_data = json.loads(str(html), encoding='utf-8')
        # json_data = json.loads(str(html), encoding='utf-8')['message'][0]['buy']
        print json_data
        # if json_data == 0:
        #     print pre_word+'.com|OK'
    except Exception, e:
        print e, u'查询失败'


if __name__ == '__main__':
    import time

    start = time.clock()
    search_domain('world')
    # with open('domain_pre.txt') as words:
    #     for i in words:
    #         print i
    #         search_domain(i)
    #         time.sleep(3)

    end = time.clock()
    print "run time is %f s" % (end - start)
